import {
  search,
  getAllBrand
} from '../../utils/api.common'

Page({

  data: {
    brandList: [],
    modelName: null,
    brandId: null,
    index: null,
    brandId: null,

  },

  // 搜索
  search() {
    if (!this.data.modelName) {
      return wx.showToast({
        title: '请输入型号',
        icon: "none",
        mask: false,
        duration: 1500
      })

    } else if (!this.data.brandId) {
      return wx.showToast({
        title: '选择品牌',
        icon: "none",
        mask: false,
        duration: 1500
      })
    }
    let params = {
      modelName: this.data.modelName,
      brandId: this.data.brandId,
    }
    search(params).then(res => {
      if (res.code === 200) {
        // 判断搜索结果
        let list = res.data || []
        if (list.length > 0) {
          wx.navigateTo({
            url: '../serchList/serchList?list=' + JSON.stringify(list),
          })
        } else {
          return wx.showToast({
            title: '搜索结果为空',
            icon: "none",
            mask: false,
            duration: 1500
          })
        }
      }
    })
  },

  // 选择品牌
  bindChange(e) {
    this.setData({
      index: +e.detail.value,
      brandId: this.data.brandList.find((x, i) => i == +e.detail.value).id
    })
  },

  // 获取品牌
  getAllBrand() {
    getAllBrand().then(res => {
      if (res.code === 200) {
        this.setData({
          brandList: res.data || []
        })
      }
    })
  },

  // 绑定型号
  inputModelName(e) {
    this.setData({
      modelName: e.detail.value
    })
  },


  // 页面加载
  onLoad: function (options) {
    this.getAllBrand()
  },

  onShareAppMessage: function (ops) {
    //转发事件来源。
    //button：页面内转发按钮；
    //menu：右上角转发菜单
    if (ops.from === 'menu') {
        var title = ops.target.dataset.title;
    };
    return {
        title: title, //转发的标题。当前小程序名称
    }
},

})